// PolicyCompute_v3

export default function (cellData, step = 4) {
  for (let i = 0; i < cellData.length; i++) {
    let cell = cellData[i];
    let sectorRequired = 1;
    let sectorReason = "最低扇区数量";
    let matchedTags = [];
    let inboundValue = 0;
    for (let j = 0; j < computingOptions.length; j++) {
      let option = computingOptions[j];
      let valueCumulated = 0;
      let valueCumulatedArr = 0;
      let valueCumulatedDep = 0;
      let valueCumulatedMisc = 0;
      let valueCumulatedDepZUUU = 0;
      let valueCumulatedDepZUTF = 0;
      let valueCumulatedDepMisc = 0;
      let valueCumulatedArrZUUU = 0;
      let valueCumulatedArrZUTF = 0;
      let valueCumulatedArrMisc = 0;
      let valueCumulatedDepZUTFtoBOKIR = 0;
      for (let airport in cell.airport) {
        if (option.airport.includes(airport)) {
          for (let type in cell.airport[airport].type) {
            if (option.type.includes(type)) {
              let total = cell.airport[airport].type[type].total;
              if (type == "ARR") {
                valueCumulatedArr += total;
                if (airport == "ZUUU") {
                  valueCumulatedArrZUUU += total;
                } else if (airport == "ZUTF") {
                  valueCumulatedArrZUTF += total;
                } else {
                  valueCumulatedArrMisc += total;
                }
              } else if (type == "DEP") {
                valueCumulatedDep += total;
                if (airport == "ZUUU") {
                  valueCumulatedDepZUUU += total;
                } else if (airport == "ZUTF") {
                  valueCumulatedDepZUTF += total;
                  // ZUTF DEP BOKIR
                  valueCumulatedDepZUTFtoBOKIR +=
                    cell.airport.ZUTF.type.DEP.point.BOKIR;
                } else {
                  valueCumulatedDepMisc += total;
                }
              } else {
                valueCumulatedMisc += total;
              }
            }
          }
        }
      }

      // V2
      // 计算步长
      valueCumulatedDep = valueCumulatedDep * (4 / step);
      valueCumulatedArr = valueCumulatedArr * (4 / step);

      // V3
      // 计算机场步长
      valueCumulatedDepZUUU = valueCumulatedDepZUUU * (4 / step);
      valueCumulatedDepZUTF = valueCumulatedDepZUTF * (4 / step);
      valueCumulatedDepMisc = valueCumulatedDepMisc * (4 / step);
      valueCumulatedArrZUUU = valueCumulatedArrZUUU * (4 / step);
      valueCumulatedArrZUTF = valueCumulatedArrZUTF * (4 / step);
      valueCumulatedArrMisc = valueCumulatedArrMisc * (4 / step);

      // V3
      // 分机场修正进离港数值
      const intenseAirportDepThreshold = 24;
      const intenseAirportDepMultipler = 1.3;
      const intenseAirportDepMultipler_ZUTFtoBOKIR = 1.5; // ZUTF DEP BOKIR
      const intenseAirportDepThresholdLow = 11;
      const intenseAirportDepMultiplerLow = 0;
      const intenseAirportArrThreshold = 29;
      const intenseAirportArrMultipler = 1.2;
      const intenseAirportArrThresholdLow = 6;
      const intenseAirportArrMultiplerLow = 0;

      // 任意机场离港大于24（不含），离港量乘以1.3
      // 任意机场进港大于29（不含），离港量乘以1.1
      // 任意机场离港小于11（不含），离港量乘以0
      // 任意机场进港小于6（不含），离港量乘以0
      // 天府出BOKIR增加一倍（即乘以2）

      valueCumulatedDepZUUU =
        valueCumulatedDepZUUU > intenseAirportDepThreshold
          ? valueCumulatedDepZUUU * intenseAirportDepMultipler
          : valueCumulatedDepZUUU < intenseAirportDepThresholdLow
          ? valueCumulatedDepZUUU * intenseAirportDepMultiplerLow
          : valueCumulatedDepZUUU;
      valueCumulatedDepZUTF =
        valueCumulatedDepZUTF > intenseAirportDepThreshold
          ? valueCumulatedDepZUTF * intenseAirportDepMultipler
          : valueCumulatedDepZUTF < intenseAirportDepThresholdLow
          ? valueCumulatedDepZUTF * intenseAirportDepMultiplerLow
          : valueCumulatedDepZUTF;
      valueCumulatedDepZUTFtoBOKIR =
        valueCumulatedDepZUTFtoBOKIR * intenseAirportDepMultipler_ZUTFtoBOKIR;

      valueCumulatedArrZUUU =
        valueCumulatedArrZUUU > intenseAirportArrThreshold
          ? valueCumulatedArrZUUU * intenseAirportArrMultipler
          : valueCumulatedArrZUUU < intenseAirportArrThresholdLow
          ? valueCumulatedArrZUUU * intenseAirportArrMultiplerLow
          : valueCumulatedArrZUUU;
      valueCumulatedArrZUTF =
        valueCumulatedArrZUTF > intenseAirportArrThreshold
          ? valueCumulatedArrZUTF * intenseAirportArrMultipler
          : valueCumulatedArrZUTF < intenseAirportArrThresholdLow
          ? valueCumulatedArrZUTF * intenseAirportArrMultiplerLow
          : valueCumulatedArrZUTF;

      valueCumulatedDep =
        valueCumulatedDepZUUU +
        valueCumulatedDepZUTF +
        valueCumulatedDepMisc +
        valueCumulatedDepZUTFtoBOKIR;
      valueCumulatedArr =
        valueCumulatedArrZUUU + valueCumulatedArrZUTF + valueCumulatedArrMisc;

      // V2
      // 进离港权重计算
      const basicMultiplerArr = 1.5;
      const basicMultiplerDep = 0.5;

      valueCumulatedDep = valueCumulatedDep * basicMultiplerDep;
      valueCumulatedArr = valueCumulatedArr * basicMultiplerArr;
      valueCumulated =
        valueCumulatedDep + valueCumulatedArr + valueCumulatedMisc;

      // V2
      // 扇区数分段计算

      // 扇区数分段计算 TOP
      const optThresholdTop = 92;
      const optMultiplerArrTop = 0.9;
      const optMultiplerDepTop = 0.9;

      // 扇区数分段计算 BOTTOM
      const optThresholdBottom = 90;
      const optMultiplerDepBottom = 1;
      const optMultiplerArrBottom = 1;

      if (valueCumulated < optThresholdBottom) {
        valueCumulated =
          optMultiplerDepBottom * valueCumulatedDep +
          optMultiplerArrBottom * valueCumulatedArr +
          valueCumulatedMisc;
      } else if (valueCumulated > optThresholdTop) {
        valueCumulated =
          optMultiplerDepTop * valueCumulatedDep +
          optMultiplerArrTop * valueCumulatedArr +
          valueCumulatedMisc;
      } else {
      }

      // V1
      // 按量计算
      for (let k = 0; k < option.threshold.length; k++) {
        let threshold = option.threshold[k];
        let tagsValid = true;
        if (threshold.requiredTags && threshold.requiredTags.length > 0) {
          // match Tags
          for (let t = 0; t < threshold.requiredTags.length; t++) {
            if (!matchedTags.includes(threshold.requiredTags[t])) {
              tagsValid = false;
              break;
            }
          }
        }
        if (
          tagsValid &&
          valueCumulated >= threshold.valueCumulated &&
          sectorRequired >= threshold.sectorAlready &&
          sectorRequired < threshold.sectorRequired
        ) {
          sectorRequired = threshold.sectorRequired;
          sectorReason = option.description;
          inboundValue = valueCumulated;
          matchedTags.push(option.tag);
          // console.log(cell.moment, option.description, sectorRequired);
        }
      }
    }
    cellData[i]["sectorRequire"] = sectorRequired;
    cellData[i]["sectorReason"] = sectorReason;
    cellData[i]["inboundValue"] = (inboundValue / 4) * step;
    cellData[i]["false"];
  }
  return postProcess(cellData);
  // return cellData;
}

function postProcess(cellData) {
  if (!cellData || cellData.length == 0) {
    return cellData;
  } else {
    for (let i = 0; i < cellData.length; i++) {
      if (i > 0 && i < cellData.length - 1) {
        if (
          cellData[i].sectorRequire < cellData[i - 1].sectorRequire &&
          cellData[i].sectorRequire < cellData[i + 1].sectorRequire
        ) {
          let sectorRequireOrigional = cellData[i].sectorRequire;
          cellData[i].sectorRequire = Math.min(
            cellData[i - 1].sectorRequire,
            cellData[i + 1].sectorRequire
          );
          cellData[i].sectorReason =
            `该时刻实际建议${sectorRequireOrigional}扇区，为避免频繁开合扇，采取相邻时刻扇区建议；` +
            cellData[i].sectorReason;
          cellData[i].smoothed = true;
          // console.log(
          //   "扇区平滑",
          //   cellData[i + 1].moment,
          //   cellData[i + 1].sectorReason
          // );
        }
      }
    }
    return cellData;
  }
}

// V1
// traditional params
const computingOptions = [
  {
    description: "120=8扇; 95=7扇; 85=6扇; 75=5扇; 63=4扇; 45=3扇; 21=2扇",
    tag: "total_general_compute",
    airport: ["ZUUU", "ZUTF", "ZUMY"],
    type: ["ARR", "DEP"],
    threshold: [
      {
        valueCumulated: 120,
        sectorAlready: 0,
        sectorRequired: 8,
        requiredTags: [],
      },
      {
        valueCumulated: 95,
        sectorAlready: 0,
        sectorRequired: 7,
        requiredTags: [],
      },
      {
        valueCumulated: 85,
        sectorAlready: 0,
        sectorRequired: 6,
        requiredTags: [],
      },
      {
        valueCumulated: 75,
        sectorAlready: 0,
        sectorRequired: 5,
        requiredTags: [],
      },
      {
        valueCumulated: 63,
        sectorAlready: 0,
        sectorRequired: 4,
        requiredTags: [],
      },
      {
        valueCumulated: 45,
        sectorAlready: 0,
        sectorRequired: 3,
        requiredTags: [],
      },
      {
        valueCumulated: 21,
        sectorAlready: 0,
        sectorRequired: 2,
        requiredTags: [],
      },
      {
        valueCumulated: 0,
        sectorAlready: 0,
        sectorRequired: 1,
        requiredTags: [],
      },
    ],
  },
];
